<?php

/**
 * This is the model class for table "{{menu}}".
 *
 * The followings are the available columns in table '{{menu}}':
 * @property integer $id
 * @property string $label
 * @property string $description
 * @property string $menu_url
 * @property string $menu_group
 * @property integer $menu_order
 * @property integer $user_level
 * @property integer $is_login
 */
class MenuModel extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return MenuModel the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{menu}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('menu_order, user_level, is_login', 'numerical', 'integerOnly' => true),
            array('label', 'length', 'max' => 150),
            array('menu_url, menu_group', 'length', 'max' => 50),
            array('description', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, label, description, menu_url, menu_group, menu_order, user_level, is_login, parent_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'label' => 'Label',
            'description' => 'Description',
            'menu_url' => 'Menu Url',
            'menu_group' => 'Menu Group',
            'menu_order' => 'Menu Order',
            'user_level' => 'User Level',
            'is_login' => 'Is Login',
            'parent_id' => 'Parent'
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search($group='') {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('label', $this->label, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('menu_url', $this->menu_url, true);
        $criteria->compare('menu_group', $this->menu_group, true);
        $criteria->compare('menu_order', $this->menu_order);
        $criteria->compare('user_level', $this->user_level);
        $criteria->compare('is_login', $this->is_login);
        $criteria->compare('parent_id', $this->parent_id);

        if ($group) {

            $criteria->condition = 'menu_group=:label';
            $criteria->params = array(':label' => $group);
        }
        
        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'sort' => array(
                        'defaultOrder' => 't.menu_order asc',
                    ),
                    'pagination' => array(
                        'pageSize' => Common::getOptionValue('itemperpage','pagination'),
                    ),
                    
                ));
    }

    public function getMenuItemByGroup($group) {
        $criteria = new CDbCriteria;
        if (Yii::app()->user->isGuest) {
            $criteria->condition = 'menu_group=:group AND is_login!=:is_login';
            $criteria->params = array(':group' => $group, ':is_login' => 2);
        } else {
            $criteria->condition = 'menu_group=:group AND is_login!=:is_login';
            $criteria->params = array(':group' => $group, ':is_login' => 1);
        }
        
        $criteria->order = 'menu_order';
        $menu = $this->findAll($criteria);

        foreach ($menu as $item) {
            $criteria = new CDbCriteria;
            $criteria->condition = 'parent_id=:pid';
            $criteria->params = array(':pid' => $item->id);
            $child = MenuModel::model()->findAll($criteria);
            if(count($child)){
                 foreach ($child as $childitem) {
                     $submenulist[] = array('label' => $childitem->label, 'url' => array($childitem->menu_url));
                 }
                
                  $menulist[] = array('label' => $item->label, 'url' => array($item->menu_url), 'items'=>$submenulist );
                 unset($submenulist);
                
            }else
                $menulist[] = array('label' => $item->label, 'url' => array($item->menu_url));
        }
        return $menulist;
    }
    public function getLabel($parent){
     
        if($parent == 0){
            $parent = 'No';
        }else{
            $parent = $this->findByPk($parent)->label;
        }
        return $parent;
    }
    

}